//
//  HQYSearchModel.m
//  Go!
//
//  Created by rimi on 16/7/14.
//  Copyright © 2016年 黄秋燕. All rights reserved.
//

#import "HQYSearchModel.h"
#import "NetWorkManager.h"

@interface HQYSearchModel ()
@property (nonatomic, strong) NSMutableArray *dataSourceArray; /**< 数据源 */
@end

@implementation HQYSearchModel

- (void)getListWithKeyWords:(NSString *)keyWords {
    _dataSourceArray = [@[] mutableCopy];
    // 改变self的弱引用
    __weak typeof(self) weakSelf = self;
    
    // 获取团单网络请求
    [NetWorkManager getWithURL:@"searchshops" urlParam:@{@"city_id":[[NSUserDefaults standardUserDefaults] objectForKey:currentCity_ID], @"keyword":keyWords} successBlock:^(id responseObject) {
        if ([responseObject[@"data"] isEqual:[NSNull null]]) {
            weakSelf.response = nil;
        }
        else {
            weakSelf.response = responseObject[@"data"][@"shops"];
        }
    } FailureBlock:nil];
}


- (NSInteger)getNumberOfRows {
    return self.dataSourceArray.count;
}


- (NSArray *)getDataArray {
    if (self.response) {
        _dataSourceArray = [@[] mutableCopy];
        NSArray *array = self.response;
        for (NSDictionary *obj in array) {
            NSMutableDictionary *dataSourceDic = [NSMutableDictionary dictionary];
            NSArray *shop = obj[@"deals"];
            NSMutableArray *shopArray = [NSMutableArray array];
            for (int i = 0; i < shop.count; i++) {
                NSString *foodId = shop[i][@"deal_id"];
                NSString *name = shop[i][@"min_title"];
                NSString *icon = shop[i][@"tiny_image"];
                NSString *price = [NSString stringWithFormat:@"%.2f",
                                   ([shop[i][@"current_price"] floatValue] / 100)];
                NSString *sellNum = [NSString stringWithFormat:@"%@",shop[i][@"sale_num"]];
                NSString *url = shop[i][@"deal_murl"];
                NSDictionary *deals = @{@"name":name, @"foodId":foodId, @"icon":icon, @"price":price, @"sellNum":sellNum, @"url":url};
                [shopArray addObject:deals];
            }
            [dataSourceDic setObject:shopArray forKey:@"shops"];
            [dataSourceDic setObject:obj[@"shop_id"] forKey:@"shopId"];
            [dataSourceDic setObject:obj[@"shop_name"] forKey:@"shopName"];
            
            [dataSourceDic setObject:[NSString stringWithFormat:@"%@", obj[@"deal_num"]] forKey:@"dealNum"];
            
            [self.dataSourceArray addObject:dataSourceDic];
        }
    }
    else {
        self.dataSourceArray = nil;
    }
    
    return [self.dataSourceArray copy];
}

@end
